/*
 * #%L
 * InterfaceAPI
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.ractoc.opengamefinder.api.network;

import com.jme3.network.AbstractMessage;
import com.ractoc.opengamefinder.api.results.MessageResultsErrorCodes;

/**
 * Basic network message. This is the message all other network messaes extend
 * from.
 * 
 * @author ractoc
 * @version 0.1
 */
public class ClientServerMessage extends AbstractMessage {

    private MessageResultsErrorCodes errorCode;
    private String originPlugin;
    private String destinationPlugin;

    /**
     * Default constructor.
     */
    public ClientServerMessage() {
    }

    /**
     * Sets the destination plugin. This is the plugin on either the client on
     * the server this message is intended for.
     * 
     * @param destinationPluginParam
     *            The destination plugin this message is intended for.
     */
    public final void setDestinationPlugin(final String destinationPluginParam) {
        this.destinationPlugin = destinationPluginParam;
    }

    /**
     * Gets the destination plugin. This is the plugin on either the client on
     * the server this message is intended for.
     * 
     * @return destinationPluginParam The destination plugin this message is
     *         intended for.
     */
    public final String getDestinationPlugin() {
        return destinationPlugin;
    }

    /**
     * Sets the origin plugin. This is the plugin on either the client on the
     * server this message came from.
     * 
     * @param originPluginParam
     *            The destination plugin this message came from.
     */
    public final void setOriginPlugin(final String originPluginParam) {
        this.originPlugin = originPluginParam;
    }

    /**
     * Gets the origin plugin. This is the plugin on either the client on the
     * server this message came from.
     * 
     * @return originPluginParam The destination plugin this message came from.
     */
    public final String getOriginPlugin() {
        return originPlugin;
    }

    /**
     * Sets the error code. This is also possible to do via a message instead of
     * just via message results because the client-server communication can be
     * done asynchronous.
     * 
     * @param errorCodeParam The error code.
     */
    public final void setErrorCode(final Enum errorCodeParam) {
        this.errorCode = (MessageResultsErrorCodes)errorCodeParam;
    }

    /**
     * Gets the error code. This is also possible to do via a message instead of
     * just via message results because the client-server communication can be
     * done asynchronous.
     * 
     * @return errorCodeParam The error code.
     */
    public final Enum getErrorCode() {
        return errorCode;
    }

    /**
     * {@inheritDoc}
     * @return 
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ClientServerMessage other = (ClientServerMessage) obj;
        if (this.errorCode != other.errorCode) {
            return false;
        }
        if ((this.originPlugin == null) ? (other.originPlugin != null) : !this.originPlugin.equals(other.originPlugin)) {
            return false;
        }
        if ((this.destinationPlugin == null) ? (other.destinationPlugin != null) : !this.destinationPlugin.equals(other.destinationPlugin)) {
            return false;
        }
        return true;
    }
    
    /**
     * {@inheritDoc}
     * @return 
     */
    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    /**
     * {@inheritDoc}
     * @return 
     */
    @Override
    public String toString() {
        return this.getClass().getName() + "[originPlugin:" + originPlugin + "; destinationPlugin:" + destinationPlugin + "; errorCode:" + errorCode + "]";
    }
}
